
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <net/if.h>
#include <signal.h>
#include <sys/ioctl.h>

static char ppp_apn[100];
static char ppp_username[100];
static char ppp_password[100];
static char ppp_tel[100];
static char GET_DC_ROOT[100];

const char PPP_DEV[]        = "ppp0";
const char PPPD_NAME[]      = "pppd";
const char SCRIPT_PPP_ON[]  = "/ppp/ppp-on";
const char SCRIPT_PPP_OFF[] = "/ppp/ppp-off";

const int MAX_REDIAL      = 10;
const int MAX_GPRS_RESET  = 8;

int g_iDialConter = 0;
int g_iResetConter = 0;
int g_iPID = -1;

int g_pppSocket = -1;
time_t g_lastRecv = 0;



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
 
 
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/mii.h>
#include <linux/sockios.h>
#include <errno.h>
 
#include <ifaddrs.h>
#include <arpa/inet.h> 
 
#include  <linux/ethtool.h>
 
int cshell_netlink_status(const char *if_name)
{
	char    buffer[BUFSIZ];
	char    cmd[100];
	FILE    *read_fp;
	int        chars_read;
	int        ret =0;
 
	memset( buffer, 0, BUFSIZ );
	memset( cmd, 0, 100 );
	sprintf(cmd, "ifconfig -a | grep %s",if_name);
	read_fp = popen(cmd, "r");
	if ( read_fp != NULL )
	{
		chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
		pclose(read_fp);
 
		if (chars_read > 0)
		{
			ret = 1;
		}
		else
		{
			fprintf(stderr, "DEVICE_NONE\r\n");
			return 0;
		}
	}
 
	if(ret == 1)
	{
		memset( buffer, 0, BUFSIZ );
		memset( cmd, 0, 100 );
		sprintf(cmd, "ifconfig |grep %s",if_name);
		read_fp = popen(cmd, "r");
		if ( read_fp != NULL )
		{
			chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
			pclose(read_fp);
 
			if (chars_read > 0)
			{
				ret = 2;
			}
			else
			{
				fprintf(stderr, "DEVICE_DOWN\r\n");
			return 1;
			}
		}
	}
 
	if(ret == 2)
	{
		memset( buffer, 0, BUFSIZ );
		memset( cmd, 0, 100 );
		sprintf(cmd, "ifconfig %s | grep RUNNING |  awk '{print $3}'",if_name);
		read_fp = popen(cmd, "r");
		if ( read_fp != NULL )
		{
		    chars_read = fread(buffer, sizeof(char), BUFSIZ-1, read_fp);
		    pclose(read_fp);
			
		    if (chars_read > 0)
		    {
				fprintf(stderr, "DEVICE_LINKED\r\n");
				return 3;
		    }
		    else
		    {
				fprintf(stderr, "DEVICE_UNPLUGGED\r\n");
				return 2;
		    }
		}
	}
 
	return -1;
}
 
 
int c_netlink_status(const char *if_name )
{
	int fd = -1; 
	struct ifreq ifr; 
 
	struct ifconf ifc;  
	struct ifreq ifrs_buf[100]; 
	int if_number =0;
	int i;
 
 
	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
	{
		fprintf(stderr, "%s: socket error [%d] %s\r\n",if_name, errno, strerror(errno));
		close(fd);
		return -1; 
	}
 
	ifc.ifc_len = sizeof(ifrs_buf);  
	ifc.ifc_buf = (caddr_t)ifrs_buf;  
	if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) <0)  
	{
		fprintf(stderr, "%s: ioctl SIOCGIFCONF error [%d] %s\r\n",if_name, errno, strerror(errno));
		close(fd);
		return -1; 
	}
 
	if_number = ifc.ifc_len / sizeof(struct ifreq);
	for(i=0; i< if_number; i++)
	{
		if(strcmp(if_name,ifrs_buf[i].ifr_name ) == 0)
		{
			break;
		}
	}
 
	if(i >= if_number)
	{
		close(fd);
		fprintf(stderr, "DEVICE_NONE\r\n");
		return 0;
	}
	
	bzero(&ifr, sizeof(ifr));
	strncpy(ifr.ifr_name, if_name, IFNAMSIZ-1); 
	ifr.ifr_name[IFNAMSIZ-1] = 0; 
	if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) <0)  
	{
		fprintf(stderr, "%s: ioctl SIOCGIFFLAGS error [%d] %s\r\n",if_name, errno, strerror(errno));
		close(fd);
		return -1; 
	}
#if 1	
	if(!(ifr.ifr_flags & IFF_UP))
	{
		close(fd);
		fprintf(stderr, "DEVICE_DOWN\r\n");
		return 1;
	}
	
	if(!(ifr.ifr_flags & IFF_RUNNING))
	{
		close(fd);
		fprintf(stderr, "DEVICE_UNPLUGGED\r\n");
		return 2 ;
	}
 
	fprintf(stderr, "DEVICE_LINKED\r\n");
	return 3;
 
#else
{
	struct ethtool_value edata;
	if(!(ifr.ifr_flags & IFF_UP) || !(ifr.ifr_flags & IFF_RUNNING))
	{
		close(fd);
		fprintf(stderr, "%s: DOWN\r\n",if_name);
		return 1;
	}
	edata.cmd = ETHTOOL_GLINK;
	edata.data = 0;
	ifr.ifr_data = (char *) &edata;
	if(ioctl( fd, SIOCETHTOOL, &ifr ) < 0)
	{
		fprintf(stderr, "%s: ioctl SIOCETHTOOL error [%d] %s\r\n",if_name, errno, strerror(errno));
		close(fd);
		return -1; 
	}
 
	if(edata.data == 0)
	{
		fprintf(stderr, "DEVICE_UNPLUGGED\r\n");
		return 2; 
	}
	else
	{
		fprintf(stderr, "DEVICE_LINKED\r\n");
		return 3; 
	}
}
#endif
}



static void setPPPParameters()
{
    snprintf(ppp_apn,sizeof(ppp_apn)-1,"%s",getenv("APN"));
    snprintf(ppp_username,sizeof(ppp_username)-1,"%s",getenv("PPP_USER"));
    snprintf(ppp_password,sizeof(ppp_password)-1,"%s",getenv("PPP_PASSWORD"));
    snprintf(ppp_tel,sizeof(ppp_tel)-1,"%s",getenv("PPP_TEL"));
    snprintf(GET_DC_ROOT,sizeof(GET_DC_ROOT)-1,"%s",getenv("DC_ROOT"));
}

static void callPPP_ON()
{
    static int g_iDialConter=0;
    if (g_iDialConter < MAX_REDIAL)
    {
        char szTmp[128];
        snprintf(szTmp, sizeof(szTmp), "%s%s %s %s %s %s",
                GET_DC_ROOT,
                SCRIPT_PPP_ON,
                ppp_apn,
                ppp_username,ppp_password,ppp_tel);
        printf("ppp,call[%d:%d] (%s)", g_iDialConter, g_iResetConter, szTmp);
        system("killall -9 pppd");
        sleep(1);
        system(szTmp);
        g_iDialConter += 1;
        g_iResetConter++;
    }
    else
    {
       // need to reboot
    }
}


void *pppThreadRunner(void *arg)
{
    int sp = 200000;
    setPPPParameters();
    for (;;)
    {

        //if (!isNetDevExist(PPP_DEV))
        if(3!=cshell_netlink_status(PPP_DEV))
        {
            static int Debug = 0;
            if ((Debug++ % 3) == 0)
            {

                printf("ppp,%s is up. reset conunter", PPP_DEV);
                callPPP_ON();
            }
        }

        if(g_iResetConter>20)
        {
            // 连续拨号失败次数很多了,应该重启
        }
        usleep(sp);
    }
}